<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/** 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Library.Translation
 */
class Dmf_Translation_Lang
{
    /**
     * The language
     * 
     * @var type 
     */
    private $_lang;
    
    /**
     * All language content
     * 
     * @var array 
     */
    private $_content;
    
    /**
     * The language
     * 
     * @param string $lang 
     */
    public function __construct( $lang )
    {
        $this->_lang = $lang;
    }
    
    /**
     * Get an language item
     * 
     * @param string $index
     * @return string 
     */
    public function get( $index )
    {
        if ( ! isset( $this->_content[ $index ] ) )
        {
            return null;
        }
        
        return $this->_content[ $index ];
    }
    
    /**
     * Add translation content
     * 
     * @param array $array 
     */
    public function add( $array )
    {
        $this->_content = array_merge( $this->_content, $array );
    }
    
    /**
     * Add a language file
     * 
     * @param string $file
     * @param string $type
     * @return Dmf_Translation_Lang
     * @throws Exception 
     */
    public function addFile( $file, $type = 'php' )
    {
        if ( ! file_exists( $file ) )
        {
            throw new Dmf_Exception_FileNotFound( 'Translation file "' . $file . '" not found.' );
        }
        
        switch( $type )
        {
            case 'php':
                include( $file );
                
                if ( ! isset( $lang ) )
                {
                    throw new Exception( 'Lang array not found in translation file "' . $file . '".' );
                }
                
                $this->_content = $lang;
            break;
            case 'ini':
                $this->_content = parse_ini_file( $file, true );
            break;
        }
        
        return $this;
    }
}